/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-9-18 下午3:32 Created by dwzhang
 */
package com.jade.bss.weixin.api.protocol.xml;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

import com.jade.framework.base.util.UniqueID;
import com.xunton.bss.weixin.api.Utils;

/**
 * 通用的请求．
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-9-18 下午3:32
 */
@XmlAccessorType (XmlAccessType.FIELD)
public class Request
        implements Serializable
{
    protected Map<String, Object> params = new HashMap<String, Object>();
    //微信分配的公众账号ID（企业号corpid即为此appId）
    @XmlElement (name = "appid")
    private String appId;
    //微信分配的公众账号ID（企业号corpid即为此appId）,针对红包的情况。
    @XmlElement (name = "wxappid")
    private String wxAppId;
    //微信支付分配的商户号
    @XmlElement (name = "mch_id")
    private String mchId;
    //签名，详见签名生成算法
    @XmlElement (name = "sign")
    private String sign;
    //随机字符串，不长于32位。推荐随机数生成算法
    @XmlElement (name = "nonce_str")
    private String nonceStr;

    public String getNonceStr()
    {
        return nonceStr;
    }

    public Request()
    {
        nonceStr = UniqueID.nextCode(32, true);
        params.put("nonce_str", nonceStr);
    }

    public String getAppId()
    {
        return appId;
    }

    public void setAppId(String appId)
    {
        this.appId = appId;
        params.put("appid", appId);
    }

    public String getWxAppId()
    {
        return wxAppId;
    }

    public void setWxAppId(String wxAppId)
    {
        this.wxAppId = wxAppId;
        params.put("wxappid", wxAppId);
    }

    public String getMchId()
    {
        return mchId;
    }

    public void setMchId(String mchId)
    {
        this.mchId = mchId;
        params.put("mch_id", mchId);
    }

    public String getSign()
    {
        return sign;
    }

    public void createSign(String apiKey)
            throws Exception
    {
        sign = Utils.md5Signature(params, apiKey);
    }

    protected void putParam(String key, Object value)
    {
        params.put(key, value);
    }
}
